using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.TextEditor.Properties;

namespace ICSharpCode.TextEditor
{
    public class HRuler : Control
    {
        private TextArea textArea;

        private StringFormat stringFormat;

        public HRuler(TextArea textArea)
        {
            this.textArea = textArea;
            stringFormat = new StringFormat
            {
                Alignment = StringAlignment.Center
            };
            SetStyle(ControlStyles.OptimizedDoubleBuffer, value: true);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            HighlightColor colorFor = textArea.Document.HighlightingStrategy.GetColorFor("HRuler");
            int num = 0;
            checked
            {
                int num2 = textArea.TextView.DrawingPosition.Left + 3;
                int num3 = textArea.TextView.DrawingPosition.Right - 3;
                using (Brush brush = new SolidBrush(Color.FromArgb(160, colorFor.BackgroundColor)))
                {
                    graphics.FillRectangle(brush, base.ClientRectangle);
                }
                Color color = colorFor.Color;
                string themeName = textArea.Document.ThemeName;
                if (!(themeName == "Black"))
                {
                    if (themeName == "Default")
                    {
                        color = SystemColors.ControlLight;
                    }
                }
                else
                {
                    color = Color.FromArgb(93, 140, 201);
                }
                graphics.DrawLine(new Pen(color), new Point(0, 0), new Point(base.Width, 0));
                if (textArea.Document.TextEditorProperties.ShowLineNumbers)
                {
                    int num4 = textArea.TextView.DrawingPosition.Left - 18;
                    graphics.DrawImage(rect: new Rectangle(num4, 6, 16, 16), image: Resources.FaceMonkey);
                }
                using (Pen pen = new Pen(colorFor.Color, 1f))
                {
                    for (float num5 = num2; num5 < (float)num3; num5 += (float)textArea.TextView.WideSpaceWidth)
                    {
                        int num6;
                        unchecked
                        {
                            num6 = checked(base.Height * 2) / 3;
                            if (num % 5 == 0)
                            {
                                num6 = checked(base.Height * 4) / 5;
                            }
                            if (num % 10 == 0)
                            {
                                num6 = base.Height / 2;
                            }
                        }
                        num++;
                        graphics.DrawLine(pen, (int)num5, base.Height - 4, (int)num5, num6 - 4);
                    }
                    graphics.DrawLine(new Pen(Color.FromArgb(100, pen.Color), 1f)
                    {
                        DashStyle = DashStyle.Solid
                    }, textArea.TextView.DrawingPosition.Left, base.Height - 1, textArea.TextView.DrawingPosition.Right, base.Height - 1);
                }
            }
        }
    }
}
